/*
 * @Author: your name
 * @Date: 2022-01-01 11:00:15
 * @LastEditTime: 2022-01-02 00:15:37
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \json-serveg:\桌面2\TypeScript\part2\src\moduls\GameController.ts
 */
//引入其它的类
// import Snake from "./Snake";
// import Food from "./Food";
// import ScorePanel from "./ScorePanel";

// 引入其他的类
import Snake from "./Snake";
import Food from "./Food";
import ScorePanel from "./ScorePanel";

class GameControll{
    //定义三个属性
    //蛇
    snake:Snake
    food:Food
    scorePanel:ScorePanel

    //创建一个属性来储存蛇的移动方向(也就是按键的方向)
    direction:string = ''

    //创建一个属性用来记录游戏是否结束
    isLive = true


    constructor(){
        this.snake = new Snake()
        this.food = new Food()
        this.scorePanel = new ScorePanel()

        this.init()

    }

    //游戏的初始化
    init(){
        // console.log(1111)
        document.addEventListener('keydown',this.keydownHandler.bind(this))
        //调用run方法使用蛇可以移动
        this.run()

    }

    keydownHandler(event:KeyboardEvent){
        // console.log(this)
        // console.log(event)
        //修改direction属性的值
        this.direction = event.key
        // this.run()
    }

    // sey(){
    //     console.log(11111)
    // }

    //创建一个控制蛇移动的方法
    run(){
        //先获取蛇的坐标(现在)
        let x = this.snake.x
        let y = this.snake.y

        //根据按键的放心来修改x值和y值
        switch (this.direction) {
            case "ArrowUp":
                case "Up":
                    // 向上移动 top 减少
                    y -= 10;
                    break;
                case "ArrowDown":
                case "Down":
                    // 向下移动 top 增加
                    y += 10;
                    break;
                case "ArrowLeft":
                case "Left":
                    // 向左移动 left 减少
                    x -= 10;
                    break;
                case "ArrowRight":
                case "Right":
                    // 向右移动 left 增加
                    x += 10;
                    break;
            }

            //检测蛇是否吃到了食物
            this.checkEat(x,y)



            try {
                //修改蛇的值
                this.snake.x = x   
                this.snake.y = y    
            } catch (error) {
                // console.log(error)
                alert(error.message + 'GAME OVER')
                this.isLive = false

            }

           

            //开启一个定时器
            this.isLive && setTimeout(this.run.bind(this), 300 -(this.scorePanel.level-1)*30);

    }


    //定义一个方法,用来检测蛇是否吃到了食物
    checkEat(x:number,y:number){
        if (x === this.food.x && y === this.food.y) {
           
                // 食物的位置重置
                this.food.change()
                // 分数增加一节
                this.scorePanel.addScore()
                // 等级增加
                // this.scorePanel.addLevel()
                //蛇神自增一节
                this.snake.addBody()
        }
        
    }



}


export default GameControll